package be.nepherte.movmanager.client.ui.components;

import java.util.Date;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasValue;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.datepicker.client.DatePicker;

/**
 * This class represents a text box in which only dates are allowed.
 * 
 * <p>
 * This file is part of MovManager.
 * </p>
 * <p>
 * MovManager is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * </p>
 * <p>
 * MovManager is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 * </p>
 * <p>
 * You should have received a copy of the GNU General Public License along with
 * MovManager. If not, see <a
 * href="http://www.gnu.org/licenses/">http://www.gnu.org/licenses</a>.
 * </p>
 * 
 * @author	Bart Verhoeven
 * @version	0.1
 */
public class DateBox extends Composite implements HasValue<Date> {
	private DateTimeFormat dateFormat;
	private final TextBox box = new TextBox();
	
	/**
	 * Creates a new date box. The used format is <tt>d MMMM yyyy</tt>.
	 */
	public DateBox() {
		super();
		dateFormat = DateTimeFormat.getFormat("d MMMM yyyy");
		initWidget(box);
		box.addClickHandler(new DateBoxHandler(this));
	}

	/**
	 * Creates a new date box that shows the given date. The used format is
	 * <tt>d MMMM yyyy</tt>.
	 * 
	 * @param	date
	 * 			the date that has to be filled in this date box
	 */
	public DateBox(Date date) {
		this();
		setValue(date);
	}
	
	@Override
	public HandlerRegistration addValueChangeHandler(ValueChangeHandler<Date> handler) {
		return addHandler(handler, ValueChangeEvent.getType());
	}

	@Override
	public Date getValue() {
		String text = box.getText();
		Date date = text.equals("") ? null : dateFormat.parse(text);
		return date;
	}

	@Override
	public void setValue(Date value) {
		setValue(value, false);
	}

	@Override
	public void setValue(Date value, boolean fireEvents) {
		box.setText(value == null ? "" : dateFormat.format(value));
		
		if (fireEvents) {
			Date oldValue = getValue();
			ValueChangeEvent.fireIfNotEqual(this, oldValue, value);
		}
	}
	
	private class DateBoxHandler implements ClickHandler {
		
		private DateBox box;
		
		public DateBoxHandler(DateBox box) {
			this.box = box;
		}
		
		private class DatePickerHandler implements ValueChangeHandler<Date> {

			private DateBox box;
			
			public DatePickerHandler(DateBox box) {
				this.box = box;
			}
			
			@Override
			public void onValueChange(ValueChangeEvent<Date> event) {
				Date date = event.getValue();
				box.setValue(date);
			}
			
		}

		@Override
		public void onClick(ClickEvent event) {
			DatePicker picker = new DatePicker();
			picker.addValueChangeHandler(new DatePickerHandler(box));
			PopupPanel pp = new PopupPanel(true);
			pp.setWidget(picker);
			pp.setPopupPosition(box.getAbsoluteLeft() + 310, box.getAbsoluteTop());
			pp.show();
		}
		
	}
}
